package cn.com.template.controller.system.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.commons.Paginator;
import cn.com.commons.utils.StringUtil;
import cn.com.dal.dataobject.hr.DeptDO;
import cn.com.dal.dataobject.hr.RoleDO;
import cn.com.service.sys.RoleService;
import cn.com.support.authentication.AuthenticationUtil;
import cn.com.support.authentication.ResourceAuthCode;
import cn.com.support.context.UserContext;
import cn.com.support.message.MessageResult;
import cn.com.support.utils.TemplateUtils;
import cn.com.template.ui.Flexigrid;
import cn.com.template.utils.FlexigridUtil;

/**
 * 
 * 角色控制器
 * 
 * @since 1.0 2012-2-21 下午11:03:42
 * @version $Id$
 * @author Microbun
 */
@Controller
@RequestMapping("/system/admin/role")
public class RoleController {

	@Autowired
	private RoleService roleService;

	/**
	 * 跳转页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping
	public ModelAndView forward(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/system/admin/role");
		return mav;
	}

	/**
	 * 插入数据
	 * 
	 * @param obj
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public MessageResult<?> save(HttpServletRequest request,
			HttpServletResponse response) {
		RoleDO role = new RoleDO();
		String id = request.getParameter("id");
		try{
			role.setId(Long.parseLong(id));
		}catch(NumberFormatException e){
			//忽略
		}
		role.setName(request.getParameter("name"));
		List<ResourceAuthCode> racl = new ArrayList<ResourceAuthCode>();
		String[] menuIds = request.getParameterValues("menuIds[]");
		if (menuIds != null) {
			ResourceAuthCode rac = null;
			for (int i = 0; i < menuIds.length; i++) {
				rac = new ResourceAuthCode(menuIds[i], ResourceAuthCode.TYPE_MENU);
				racl.add(rac);
			}
		}
		role.setAuthorization(AuthenticationUtil.bulidAuthentication(racl));
		role.setCreator(UserContext.getUsername());
		return roleService.save(role);
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public MessageResult<Integer> delete(@RequestParam("id[]") Long[] id) {
		return roleService.delete(id);
	}

	/**
	 * 查询所有
	 * 
	 * @param condition
	 *            查询条件
	 * @return
	 */
	@RequestMapping("/selectList")
	@ResponseBody
	public List<RoleDO> selectList(HttpServletRequest request,
			HttpServletResponse response, @RequestBody RoleDO obj) {
		return roleService.selectList(obj);
	}

	/**
	 * 根据ID查询
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectById")
	@ResponseBody
	public MessageResult<RoleDO> selectById(@RequestParam("id") Long id) {
		return roleService.selectById(id);
	}

	/**
	 * 分页查询
	 * 
	 * @param paginator
	 * @return
	 */
	@RequestMapping("/selectForPage")
	@ResponseBody
	public Flexigrid selectForPage(HttpServletRequest request,
			HttpServletResponse response) {
		Paginator<Object, List<RoleDO>> paginator = new Paginator<Object, List<RoleDO>>();
		int page=request.getParameter("page")==null ? 1:Integer.parseInt(request.getParameter("page"));
		paginator.setCurrentPage(page);
		paginator.setCondition(TemplateUtils
				.bulidBeanMap(request, RoleDO.class));
		roleService.selectForPage(paginator);
		return FlexigridUtil.formatFlexigridData(paginator);
	}
	
		

}
